package hibernate.dao;

import hibernate.entity.Servico;
import hibernate.entity.ServicoProduto;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class ServicoDAO {
	
	private Session session;
	
	public ServicoDAO(){
		
	}
	
	public void adiciona(Servico servico){		
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.save(servico);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
	}	

	public void altera(Servico servico){		
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.update(servico);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}
	}	

	public void remove(Servico servico){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.delete(servico);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}	
	}	
	
	public Servico importaDados(String nome){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Servico.class);
		Servico servico = null;
		
    	try{
    		criteria.add(Restrictions.le("nome", nome));
    		servico = (Servico) criteria.uniqueResult();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}				
		return servico;
	}
	
	public List<Servico> getListaServicosProdutos(){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Servico.class);		
		List<Servico> listaServicos = null;
		
    	try{
    		criteria.setFetchMode("servicosProdutos", FetchMode.JOIN);
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			listaServicos = (List<Servico>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaServicos;
	}
	
	public List<Servico> getListaServicos(){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Criteria criteria = session.createCriteria(Servico.class);
		List<Servico> listaServicos = null;
		
		try{
			listaServicos = criteria.list();
		} catch(Exception e){
			System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}
		return listaServicos;		
	}	
	
	
	public Servico getProdutosEGastos(Long id){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();
		Criteria criteria = session.createCriteria(Servico.class);
		Servico produtosEGastos = null;
		
		try{
			criteria.add(Restrictions.eq("id_servico", id));
			criteria.setFetchMode("servicosProdutos", FetchMode.JOIN);
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			produtosEGastos = (Servico) criteria.uniqueResult();
		} catch(Exception e){
			System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}
		return produtosEGastos;			
	}
	
	
}
